document.addEventListener('DOMContentLoaded', function() {
    const username = localStorage.getItem('username');
    const role = localStorage.getItem('role');
    
    if (!username || role !== 'patient') {
        window.location.href = 'index.html';
    }
    
    const aiChatBtn = document.getElementById('aiChatBtn');
    const healthDataBtn = document.getElementById('healthDataBtn');
    const callDoctorBtn = document.getElementById('callDoctorBtn');
    const logoutBtn = document.getElementById('logoutBtn');
    
    const aiChatSection = document.getElementById('aiChatSection');
    const healthDataSection = document.getElementById('healthDataSection');
    const callDoctorSection = document.getElementById('callDoctorSection');
    
    aiChatBtn.addEventListener('click', function() {
        setActiveSection(aiChatBtn, aiChatSection);
    });
    
    healthDataBtn.addEventListener('click', function() {
        setActiveSection(healthDataBtn, healthDataSection);
    });
    
    callDoctorBtn.addEventListener('click', function() {
        setActiveSection(callDoctorBtn, callDoctorSection);
    });
    
    logoutBtn.addEventListener('click', function() {
        localStorage.clear();
        window.location.href = 'index.html';
    });
    
    const sendChatBtn = document.getElementById('sendChatBtn');
    const chatInput = document.getElementById('chatInput');
    const chatMessages = document.getElementById('chatMessages');
    
    sendChatBtn.addEventListener('click', sendMessage);
    chatInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });
    
    const healthDataForm = document.getElementById('healthDataForm');
    
    healthDataForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const temperature = document.getElementById('temperature').value;
        const heartRate = document.getElementById('heartRate').value;
        const bloodPressure = document.getElementById('bloodPressure').value;
        const bloodSugar = document.getElementById('bloodSugar').value;
        
        alert('数据上传成功！');
        healthDataForm.reset();
    });
    
    const cancelCallBtn = document.getElementById('cancelCallBtn');
    
    cancelCallBtn.addEventListener('click', function() {
        setActiveSection(aiChatBtn, aiChatSection);
    });
    
    function setActiveSection(button, section) {
        document.querySelectorAll('.menu button').forEach(btn => btn.classList.remove('active'));
        document.querySelectorAll('.section').forEach(sec => sec.classList.add('hidden'));
        
        button.classList.add('active');
        section.classList.remove('hidden');
    }
    
    function sendMessage() {
        const message = chatInput.value.trim();
        if (message) {
            addMessage('user', message);
            chatInput.value = '';
            
            setTimeout(() => {
                const responses = [
                    "根据您的问题，建议采取以下护理措施：1. 保持休息；2. 按时服药；3. 定期监测体征。",
                    "您的情况需要密切观察，如有加重请及时就医。",
                    "针对您的症状，推荐采用温和的理疗方法，避免过度劳累。",
                    "建议调整饮食结构，增加营养摄入，同时保持适当运动。"
                ];
                const aiResponse = responses[Math.floor(Math.random() * responses.length)];
                addMessage('ai', aiResponse);
            }, 1000);
        }
    }
    
    function addMessage(sender, text) {
        const messageElement = document.createElement('div');
        messageElement.classList.add('chat-message', sender);
        messageElement.textContent = text;
        chatMessages.appendChild(messageElement);
        chatMessages.scrollTop = chatMessages.scrollHeight;
    }
});
